package cn.zhaopin.starter.security.token;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

public class WeWorkThirdAuthenticationToken extends AbstractAuthenticationToken {

    private static final long serialVersionUID = 2383092775910246006L;

    private final String suiteId;
    private String code;
    private String state;
    /** 登陆方式 web:企业微信内部浏览器登陆 */
    private String loginType = "web";

    public WeWorkThirdAuthenticationToken(String suiteId, String code, String loginType, String state) {
        super(null);
        this.suiteId = suiteId;
        this.code = code;
        this.loginType = loginType;
        this.state = state;
        setAuthenticated(false);
    }

    public WeWorkThirdAuthenticationToken(String suiteId, String code, String state, String loginType, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.suiteId = suiteId;
        this.code = code;
        this.loginType = loginType;
        this.state = state;
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return this.code;
    }

    @Override
    public Object getPrincipal() {
        return this.suiteId;
    }

    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        }
        super.setAuthenticated(false);
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
        this.code = null;
    }

    public String getState() {
        return this.state;
    }

    public String getLoginType() {
        return loginType;
    }
}
